﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Homework7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void btn_1_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_1.Text);
        }

        private void btn_2_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_2.Text);
        }

        private void btn_3_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_3.Text);
        }

        private void btn_4_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_4.Text);
        }

        private void btn_5_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_5.Text);
        }

        private void btn_6_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_6.Text);
        }

        private void btn_7_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_7.Text);
        }

        private void btn_8_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_8.Text);
        }

        private void btn_9_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_9.Text);
        }

        private void btn_0_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_0.Text);
        }

        private void btn_dispose_Click(object sender, EventArgs e)
        {
            this.textBox1.Clear();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_add.Text);
        }

        private void btn_sub_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_sub.Text);
        }

        private void btn_multi_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_multi.Text);
        }

        private void btn_divide_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText(this.btn_divide.Text);
        }

        private void btn_equal_Click(object sender, EventArgs e)
        {
            int i = 0;
            double result = 0;
            char icon;
            string representation = this.textBox1.Text;

            StringBuilder a = new StringBuilder("");
            StringBuilder b = new StringBuilder("");
            while (
                   representation[i]!='+' &&
                   representation[i]!='-'&&
                   representation[i]!='*'&&
                   representation[i]!='/')
            {
                a.Append(representation[i]);
                i++;
            }
            icon = representation[i];
            i++;
            while (i<representation.Length)
            {
                b.Append(representation[i]);
                i++;
            }
            this.textBox1.AppendText(this.btn_equal.Text);
            switch (icon)
            {
                case '+':
                    result = double.Parse(a.ToString())+double.Parse(b.ToString());
                    break;
                case '-':
                    result = double.Parse(a.ToString()) - double.Parse(b.ToString());
                    break;
                case '*':
                    result = double.Parse(a.ToString()) * double.Parse(b.ToString());
                    break;
                case '/':
                    result = double.Parse(a.ToString()) / double.Parse(b.ToString());
                    break;
                default:
                    break;
            }
            try {
                this.textBox1.AppendText(result.ToString());
            }
            catch(ArgumentNullException arg)
            {
                MessageBox.Show("格式有误");
            }
        }
    }
}
